package com.zero.system.controller;


import com.zero.system.entity.Customer;
import com.zero.system.entity.Flight;
import com.zero.system.entity.Order;
import com.zero.system.service.CustomerService;
import com.zero.system.service.FlightService;
import com.zero.system.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/home")
public class CommonController {

    @Autowired
    private CustomerService customerService;

    @Autowired
    private FlightService flightService;

    @Autowired
    private OrderService orderService;

    @GetMapping("/index")
    public String index(){
        return "home/index";
    }

    @GetMapping("/login")
    public String login(){
        return "home/login";
    }

    @GetMapping("/register")
    public String register(){
        return "home/register";
    }

    /**
     * 登录验证
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("/validate")
    public String validate(HttpServletRequest request){
        boolean flag=false;
        String username=request.getParameter("username");
        String password=request.getParameter("password");
        Customer customer=customerService.login(username,password);
        if(!StringUtils.isEmpty(customer)){
            request.getSession().setAttribute("customer",customer);
            flag=true;
        }
        return String.valueOf(flag);
    }

    /**
     * 登出
     * @param request
     * @return
     */
    @RequestMapping("/logout")
    public String logout(HttpServletRequest request){
        request.getSession().removeAttribute("customer");
        return "home/index";
    }

    /**
     * 注册
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("/registered")
    public String registered(HttpServletRequest request){
        boolean flag = false;
        String username=request.getParameter("username");
        String password=request.getParameter("password");
        Customer findByUserName = customerService.findByUserName(username);
        if(findByUserName != null){
            return String.valueOf(flag);
        }
        Customer customer=new Customer();
        customer.setUsername(username);
        customer.setPassword(password);
        customer.setAddDate(new Date());
        int state = customerService.add(customer);
        flag = true;
        return String.valueOf(flag);
    }

    /**
     * 航班查询
     * @param request
     * @return
     */
    @RequestMapping("/flight")
    public String flight(HttpServletRequest request) throws ParseException {
        String startPlace=request.getParameter("startPlace");
        String endPlace=request.getParameter("endPlace");
        String startDate=request.getParameter("startDate");
        String day1=startDate+" 23:59";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        List<Flight> flightList=flightService.findByCondition(startPlace,endPlace,sdf.parse(startDate+" 00:00"),sdf.parse(day1));
        System.out.println(flightList);
        request.setAttribute("flightList",flightList);
        request.setAttribute("startDate",sdf.parse(startDate+" 00:00"));
        request.setAttribute("startPlace",startPlace);
        request.setAttribute("endPlace",endPlace);
        return "home/flight";
    }

    /**
     * 预订航班
     * @param request
     * @return
     * @throws ParseException
     */
    @RequestMapping("/bookingFlight")
    public String bookingFlight(HttpServletRequest request) throws ParseException {
        Customer customer= (Customer) request.getSession().getAttribute("customer");
        if(!StringUtils.isEmpty(customer)){
            String flightId=request.getParameter("flightId");
            String price=request.getParameter("price");
            String seatType=request.getParameter("seatType");
            Flight flight=flightService.findById(Integer.parseInt(flightId));
            request.setAttribute("flight",flight);
            request.setAttribute("price",price);
            request.setAttribute("halfPrice",Integer.parseInt(price)/2);
            request.setAttribute("seatType",seatType);
            return "home/bookingFlight";
        }else {
            return "home/login";
        }
    }

    /**
     * 提交订单
     * @param request
     * @return
     */
    @RequestMapping("/addOrder")
    public String addOrder(HttpServletRequest request){
        Customer customer= (Customer) request.getSession().getAttribute("customer");
        if(!StringUtils.isEmpty(customer)){
            Order order=new Order();
            String flightId=request.getParameter("flightId");
            String price=request.getParameter("price");
            String seatType=request.getParameter("seatType");
            String customerType=request.getParameter("person");
            String address=request.getParameter("address");
            String idNumber=request.getParameter("identificationCard");
            String realName=request.getParameter("realName");
            String phoneNumber=request.getParameter("phoneNumber");
            Flight flight=flightService.findById(Integer.parseInt(flightId));
            order.setCustomer(customer);
            order.setAddDate(new Date());
            order.setPhone(phoneNumber);
            order.setAddress(address);
            order.setCustomerType(customerType);
            order.setSeatType(seatType);
            order.setCustomerIdNumber(idNumber);
            order.setFlight(flight);
            order.setCustomerName(realName);
            order.setPrice(price);
            order.setState(0);
            int status=orderService.add(order);
            request.setAttribute("flight",flight);
            request.setAttribute("price",price);
            request.setAttribute("seatType",seatType);
            request.setAttribute("phone",phoneNumber);
            request.setAttribute("idNumber",idNumber);
            request.setAttribute("customerType",customerType);
            request.setAttribute("realName",realName);
            return "home/ordershowdetail";
        }else {
            return "home/login";
        }
    }

    /**
     * 查看订单详情
     * @param request
     * @return
     */
    @RequestMapping("/orderdetail")
    public String orderdetail(HttpServletRequest request){
        String id=request.getParameter("id");
        Order order=orderService.findById(Integer.parseInt(id));
        request.setAttribute("flight",order.getFlight());
        request.setAttribute("price",order.getPrice());
        request.setAttribute("seatType",order.getSeatType());
        request.setAttribute("phone",order.getPhone());
        request.setAttribute("idNumber",order.getCustomerIdNumber());
        request.setAttribute("customerType",order.getCustomerType());
        request.setAttribute("realName",order.getCustomerName());
        return "home/ordershowdetail";
    }

    /**
     * 个人信息
     * @return
     */
    @RequestMapping("/customerinfo")
    public String customerinfo(HttpServletRequest request){
        Customer customer= (Customer) request.getSession().getAttribute("customer");
        if(!StringUtils.isEmpty(customer)){
            return "home/customerinfo";
        }else {
            return "home/login";
        }
    }

    /**
     * 修改个人信息
     * @return
     */
    @RequestMapping("/editcustomerinfo")
    public String editcustomerinfo(HttpServletRequest request){
        Customer customer= (Customer) request.getSession().getAttribute("customer");
        if(!StringUtils.isEmpty(customer)){
            return "home/editcustomerinfo";
        }else {
            return "home/login";
        }
    }

    /**
     * 我的订单
     * @return
     */
    @RequestMapping("/myorder")
    public String myorder(HttpServletRequest request){
        Customer customer= (Customer) request.getSession().getAttribute("customer");
        if(!StringUtils.isEmpty(customer)){
            List<Order> orderList=orderService.findByCustomerId(customer.getId());
            request.setAttribute("orderList",orderList);
            return "home/myorder";
        }else {
            return "home/login";
        }
    }

    /**
     * 修改个人信息
     * @param request
     * @return
     */
    @RequestMapping("/editCustomer")
    public String editCustomer(HttpServletRequest request){
        Customer customer= (Customer) request.getSession().getAttribute("customer");
        if(!StringUtils.isEmpty(customer)){
            String sex=request.getParameter("user.sex");
            String realName=request.getParameter("user.realName");
            String identificationCard=request.getParameter("user.identificationCard");
            String phoneNumber=request.getParameter("user.phoneNumber");
            String email=request.getParameter("user.email");
            customer.setSex(sex);
            customer.setRealName(realName);
            customer.setIdNumber(identificationCard);
            customer.setPhone(phoneNumber);
            customer.setEmail(email);
            customerService.update(customer);
            return "redirect:/home/customerinfo";
        }else {
            return "home/login";
        }
    }


    /**
     * 修改密码页面跳转
     * @param request
     * @return
     */
    @RequestMapping("/editcustomerpassword")
    public String editcustomerpassword(HttpServletRequest request){
        Customer customer= (Customer) request.getSession().getAttribute("customer");
        if(!StringUtils.isEmpty(customer)){
            return "/home/editcustomerpassword";
        }else {
            return "home/login";
        }
    }

    /**
     * 提交修改密码
     * @param request
     * @return
     */
    @RequestMapping("/editpwd")
    public String editpwd(HttpServletRequest request){
        Customer customer= (Customer) request.getSession().getAttribute("customer");
        if(!StringUtils.isEmpty(customer)){
            String password=request.getParameter("user.password");
            customer.setPassword(password);
            customerService.update(customer);
            return "redirect:/home/customerinfo";
        }else {
            return "home/login";
        }
    }

    /**
     * 取消与付款
     * @return
     */
    @RequestMapping("/updateState")
    public String updateState(HttpServletRequest request){
        Customer customer= (Customer) request.getSession().getAttribute("customer");
        if(!StringUtils.isEmpty(customer)){
            String id=request.getParameter("id");
            String state=request.getParameter("state");
            int status=orderService.updateState(Integer.parseInt(id),Integer.parseInt(state));
            return "redirect:/home/myorder";
        }else {
            return "home/login";
        }
    }

}
